package com.ruoyi.web.controller.system;

import java.io.FileNotFoundException;
import java.util.List;

import com.ruoyi.system.domain.Student;
import com.ruoyi.system.domain.Teacher;
import com.ruoyi.system.service.IClassService;
import com.ruoyi.system.service.IStudentService;
import com.ruoyi.system.service.ITermService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Score;
import com.ruoyi.system.service.IScoreService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 成绩 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-12-11
 */
@Controller
@RequestMapping("/system/score")
public class ScoreController extends BaseController
{
	private static final Logger log = LoggerFactory.getLogger(ScoreController.class);
    private String prefix = "system/score";
	
	@Autowired
	private IScoreService scoreService;
	@Autowired
	private IStudentService studentService;
	@Autowired
	private ITermService termService;
	@Autowired
	private IClassService classService;
	@RequiresPermissions("system:score:view")
	@GetMapping()
	public String score(ModelMap mmap)
	{
		mmap.put("students", studentService.selectStudentAll());
		mmap.put("terms", termService.selectTermAll());
		mmap.put("classes", classService.selectClassAll());
	    return prefix + "/score";
	}
	
	/**
	 * 查询成绩列表
	 */
	@RequiresPermissions("system:score:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Score score)
	{
		startPage();
        List<Score> list = scoreService.selectScoreList(score);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出成绩列表
	 */
	@RequiresPermissions("system:score:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Score score)
    {
    	List<Score> list = scoreService.selectScoreList(score);
        ExcelUtil<Score> util = new ExcelUtil<Score>(Score.class);
        return util.exportExcel(list, "score");
    }
	/**
	 * 导入成绩列表
	 */
	@Log(title = "成绩管理", businessType = BusinessType.IMPORT)
	@RequiresPermissions("system:score:importExcel")
	@PostMapping("/importExcel")
	@ResponseBody
	public AjaxResult importExcel(@RequestParam("file") MultipartFile file)
	{
		if (file.isEmpty())
		{
			return AjaxResult.error("文件不能为空");
		}
		try {
			ExcelUtil<Score> util = new ExcelUtil<Score>(Score.class);
			List<Score> scoreList=util.importExcel(file.getInputStream());
			if (scoreList.size()>0){
				for (int i=0;i<scoreList.size();i++){
					Student student=studentService.selectStudentByStuNum(scoreList.get(i).getStuNum());
					if (student!=null){
						Score score =new Score();
						score.setStuName(scoreList.get(i).getStuName());
						score.setStuNum(scoreList.get(i).getStuNum());
						score.setScoreChinese(scoreList.get(i).getScoreChinese());
						score.setScoreMath(scoreList.get(i).getScoreMath());
						score.setScoreEnglish(scoreList.get(i).getScoreEnglish());
						score.setScorePolitical(scoreList.get(i).getScorePolitical());
						score.setScoreHistory(scoreList.get(i).getScoreHistory());
						score.setScoreBiology(scoreList.get(i).getScoreBiology());
						score.setScoreGeographical(scoreList.get(i).getScoreGeographical());
						score.setScoreChemistry(scoreList.get(i).getScoreChemistry());
						score.setScorePhysical(scoreList.get(i).getScorePhysical());
						score.setClassNo(student.getClassNo());
						score.setScoreBiology(scoreList.get(i).getScoreBiology());
						scoreService.insertScore(score);
					}else {
						return AjaxResult.error(scoreList.get(i).getStuName()+"学生信息不存在，请维护");
					}

				}

			}
		} catch (FileNotFoundException e) {
			log.error("导入异常",e);
		} catch (Exception e) {
			log.error("保存数据",e);
		}
		return AjaxResult.success("导入成功");
	}
	/**
	 * 新增成绩
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		mmap.put("students", studentService.selectStudentAll());
		mmap.put("terms", termService.selectTermAll());
		mmap.put("classes", classService.selectClassAll());
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存成绩
	 */
	@RequiresPermissions("system:score:add")
	@Log(title = "成绩", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Score score)
	{
		Student student=studentService.selectStudentByStuNum(score.getStuNum());
		score.setClassNo(student.getClassNo());
		return toAjax(scoreService.insertScore(score));
	}

	/**
	 * 修改成绩
	 */
	@GetMapping("/edit/{scoreId}")
	public String edit(@PathVariable("scoreId") Integer scoreId, ModelMap mmap)
	{
		Score score = scoreService.selectScoreById(scoreId);
		mmap.put("score", score);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存成绩
	 */
	@RequiresPermissions("system:score:edit")
	@Log(title = "成绩", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Score score)
	{		
		return toAjax(scoreService.updateScore(score));
	}
	
	/**
	 * 删除成绩
	 */
	@RequiresPermissions("system:score:remove")
	@Log(title = "成绩", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(scoreService.deleteScoreByIds(ids));
	}
	
}
